﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.user_priv
{
    public partial class edit_priv : ModulePage
    {

        public string JavaVar = ""; 
        public int MenuCount;
        private string funcIdStr = ",";
        private int priv_id;
 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.priv_id = (Request.QueryString["priv_id"] == null) ? 0 : int.Parse(Request.QueryString["priv_id"].ToString());
                this.ViewState["PrivId"] = this.priv_id;
                DataTable table = new UserPrivDao().SelectByPrivId(base.Conn, this.priv_id);
                if (table.Rows.Count > 0)
                {
                    this.lblUserPriv.Text = (table.Rows[0]["PrivName"] == DBNull.Value) ? "" : table.Rows[0]["PrivName"].ToString();
                    this.funcIdStr = this.funcIdStr + ((table.Rows[0]["FuncIdStr"] == DBNull.Value) ? "" : table.Rows[0]["FuncIdStr"].ToString());
                    this.ViewState["PrivName"] = (table.Rows[0]["PrivName"] == DBNull.Value) ? "" : table.Rows[0]["PrivName"].ToString();
                    this.ViewState["PrivNo"] = (table.Rows[0]["PrivNo"] == DBNull.Value) ? "" : table.Rows[0]["PrivNo"].ToString();
                }
                DataSet dataSet = new DataSet();
                StringBuilder builder = new StringBuilder();
                builder.Append("<table border=\"0\" cellspacing=\"1\" class=\"small\" cellpadding=\"2\" align=\"center\">\n");
                string commandText = "SELECT [MenuID],[MenuName],[Image] FROM [dbo].[Sys_Menu] ORDER BY [MenuId] ASC";
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                table2.TableName = "Sys_Menu";
                if (table2.Rows.Count > 0)
                {
                    this.MenuCount = table2.Rows.Count;
                    builder.Append("<tr>\n");
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        string javaVar = this.JavaVar;
                        this.JavaVar = javaVar + "MENU_ID_ARRAY[" + i.ToString() + "]=\"" + table2.Rows[i]["MenuID"].ToString() + "\";\n";
                        builder.Append("  <td  valign=\"top\">\n");
                        builder.Append("<table border=\"0\" cellspacing=\"1\" class=\"small\" bgcolor=\"#000000\" cellpadding=\"2\" align=\"center\">\n");
                        builder.Append("<tr class=\"TableHeader\" title=\"" + table2.Rows[i]["MenuName"].ToString() + "\">\n");
                        builder.Append("  <td nowrap>\n");
                        builder.Append("<input type=\"checkbox\" name=\"MENU_" + table2.Rows[i]["MenuID"].ToString() + "\" onClick=\"check_all(this,'" + table2.Rows[i]["MenuID"].ToString() + "');\">\n");
                        builder.Append("  <img src=\"/images/menu/" + table2.Rows[i]["Image"].ToString() + ".gif\" width=19 height=17>&nbsp;&nbsp;<b>" + table2.Rows[i]["MenuName"].ToString() + "</b>\n");
                        builder.Append("  </td>\n");
                        builder.Append("</tr>\n");
                        string str3 = "select * from sys_function where len(MenuId)=4 and left(MenuID,2) ='" + table2.Rows[i]["MenuID"].ToString() + "' order by MenuID asc, FuncID asc";
                        SqlHelper.FillDataset(base.Conn, CommandType.Text, str3, dataSet, new string[] { "Func1" });
                        if (dataSet.Tables["Func1"].Rows.Count > 0)
                        {
                            for (int j = 0; j < dataSet.Tables["Func1"].Rows.Count; j++)
                            {
                                builder.Append("<tr>\n");
                                builder.Append("  <td  class=\"TableData\" nowrap>\n");
                                string str4 = (this.funcIdStr.IndexOf("," + dataSet.Tables["Func1"].Rows[j]["FuncID"].ToString() + ",") >= 0) ? " checked" : "";
                                builder.Append(string.Concat(new object[] { "<input type=\"checkbox\" name=\"", table2.Rows[i]["MenuID"].ToString(), "\" value=\"", dataSet.Tables["Func1"].Rows[j]["FuncID"], "\"", str4, ">" }));
                                str4 = "";
                                string str5 = dataSet.Tables["Func1"].Rows[j]["FuncImg"].ToString();
                                builder.Append("<img src=\"/images/menu/" + str5 + ".gif\" width=19 height=17>&nbsp;&nbsp;" + dataSet.Tables["Func1"].Rows[j]["FuncName"].ToString() + "");
                                string str6 = "select * from sys_function where len(MenuID)=6 and left(MenuID,4) ='" + ((string)dataSet.Tables["Func1"].Rows[j]["MenuID"]) + "' order by MenuID asc, FuncID asc";
                                SqlHelper.FillDataset(base.Conn, CommandType.Text, str6, dataSet, new string[] { "Func2" });
                                if (dataSet.Tables["Func2"].Rows.Count > 0)
                                {
                                    for (int k = 0; k < dataSet.Tables["Func2"].Rows.Count; k++)
                                    {
                                        str4 = (this.funcIdStr.IndexOf("," + dataSet.Tables["Func2"].Rows[k]["FuncID"].ToString() + ",") >= 0) ? " checked" : "";
                                        builder.Append(string.Concat(new object[] { "<br>&nbsp;&nbsp;<input type=\"checkbox\" name=\"", table2.Rows[i]["MenuID"].ToString(), "\" value=\"", dataSet.Tables["Func2"].Rows[k]["FuncID"], "\"", str4, ">\n" }));
                                        str4 = "";
                                        str5 = dataSet.Tables["Func2"].Rows[k]["FuncImg"].ToString();
                                        builder.Append("<img src=\"/images/menu/" + str5 + ".gif\" width=19 height=17>&nbsp;&nbsp;" + dataSet.Tables["Func2"].Rows[k]["FuncName"].ToString() + "");
                                    }
                                }
                                dataSet.Tables["Func2"].Clear();
                                builder.Append("  </td>\n");
                                builder.Append("</tr>\n");
                            }
                            dataSet.Tables["Func1"].Clear();
                        }
                        builder.Append("</table>\n");
                        builder.Append("  </td>\n");
                    }
                    builder.Append("</tr>\n");
                    builder.Append("</table>\n");
                    this.litForm.Text = builder.ToString();
                }
                else
                {
                    this.lblMessage.Text = base.ShowError("无相关角色！", "index.aspx");
                    this.pnlMain.Visible = false;
                }
            }
            if (this.Page.IsPostBack)
            {
                SqlCommand command = new SqlCommand();
                base.Conn.Open();
                command.Connection = base.Conn;
                command.CommandText = "UPDATE User_Priv SET FuncIdStr = @FuncIdStr WHERE PrivId = @PrivId";
                command.Parameters.Add("@FuncIdStr", this.lblFuncIdStr.Value);
                command.Parameters.Add("@PrivId", Convert.ToInt32(this.ViewState["PrivId"].ToString()));
                command.ExecuteNonQuery();
                this.pnlMain.Visible = false;
                this.lblMessage.Text = base.ShowPrompt("编辑权限成功!", "index.aspx");
            }

        }
    }
}
